package org.linlinjava.litemall.admin.web;

import org.linlinjava.litemall.admin.service.AdminRentOrderService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallRentOrder;
import org.linlinjava.litemall.db.dto.ReturnBookDto;
import org.linlinjava.litemall.db.service.LitemallRentOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/rentOrder")
public class AdminRentOrderController {
    @Autowired
    private AdminRentOrderService
            adminRentOrderService;
    @Autowired
    private LitemallRentOrderService rentOrderService;

    /**
     * 导出 买书/租书 订单中待发货的订单Excel
     *
     * @param type
     * @param response
     * @return
     */
    @PostMapping("/exportExcel")
    public Object exportExcel(@RequestParam String type, HttpServletResponse response) {
        return adminRentOrderService.exportExcel(type, response);
    }

    /**
     * 确认归还
     *
     * @param rentOrderId
     * @return
     */
    @PostMapping("/confirmReturn")
    public Object confirmReturn(@RequestBody Integer rentOrderId) {
        return adminRentOrderService.confirmReturn(rentOrderId);
    }

    /**
     * 后台根据姓名模糊查询和订单状态查询
     *
     * @param username
     * @param status
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/list")
    public Object querySelective(@RequestParam(required = false) String username,
                                 @RequestParam(required = false) String rentOrderSn,
                                 @RequestParam(required = false) String start,
                                 @RequestParam(required = false) String end,
                                 @RequestParam(defaultValue = "0") Integer status,
                                 @RequestParam(required = false, defaultValue = "1") Integer page,
                                 @RequestParam(required = false, defaultValue = "10") Integer limit,
                                 @RequestParam(required = false, defaultValue = "add_time") String sort,
                                 @RequestParam(required = false, defaultValue = "desc") String order,
                                 @RequestParam(required = false) String bookName,
                                 @RequestParam(required = false) Integer userId) {
        return ResponseUtil.okList(rentOrderService.querySelective(username, rentOrderSn, start, end, status, page, limit, sort, order,bookName,userId));
    }

    /**
     * 后台根据姓名模糊查询和订单状态查询
     *
     * @param username
     * @param status
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/adminList")
    public Object adminList(@RequestParam Integer userId,
            @RequestParam(required = false) String username,
                                 @RequestParam(required = false) String rentOrderSn,
                                 @RequestParam(required = false) String start,
                                 @RequestParam(required = false) String end,
                                 @RequestParam(defaultValue = "0") Integer status,
                                 @RequestParam(required = false, defaultValue = "1") Integer page,
                                 @RequestParam(required = false, defaultValue = "10") Integer limit,
                                 @RequestParam(required = false, defaultValue = "add_time") String sort,
                                 @RequestParam(required = false, defaultValue = "desc") String order,
                                 @RequestParam(required = false) String bookName) {
        return ResponseUtil.okList(rentOrderService.querySelective(userId,username, rentOrderSn, start, end, status, page, limit, sort, order,bookName));
    }


    /**
     * 查询订单详情
     *
     * @param rentOrderId
     * @return
     */
    @GetMapping("/detail")
    public Object queryDetail(@RequestParam Integer rentOrderId) {
        return ResponseUtil.ok(rentOrderService.queryRentOrderDetail(rentOrderId));
    }

    /**
     * 客户自己私下找物流或者自己上门送
     * (shipWay 1:自送，2：私下找物流，需填 快递公司代码：shipperCode，运单号：logisticCode)
     * orderId 需要传
     *
     * @param shipWay
     * @param shipperCode
     * @param logisticCode
     * @param rentOrderId
     * @return
     */
    @GetMapping("/rentSelfDelivery")
    public Object rentSelfDelivery(@RequestParam Integer shipWay, @RequestParam(defaultValue = "") String shipperCode, @RequestParam(defaultValue = "") String logisticCode, @RequestParam Integer rentOrderId) {
        return ResponseUtil.ok(adminRentOrderService.rentSelfDelivery(shipWay, shipperCode, logisticCode, rentOrderId));
    }

    /**
     * 还书 自送或自己接物流
     *
     * @param shipWay
     * @param shipperCode
     * @param logisticCode
     * @param rentOrderId
     * @return
     */
    @GetMapping("/rentBackSelfDelivery")
    public Object rentBackSelfDelivery(@RequestParam Integer shipWay, @RequestParam(defaultValue = "") String shipperCode, @RequestParam(defaultValue = "") String logisticCode, @RequestParam Integer rentOrderId) {
        return ResponseUtil.ok(adminRentOrderService.rentBackSelfDelivery(shipWay, shipperCode, logisticCode, rentOrderId));
    }

    /**
     * 获取租书订单物流轨迹
     *
     * @param rentOrderId
     * @return
     */
    @GetMapping("/getExpressTrace")
    public Object getExpressTrace(@RequestParam Integer rentOrderId) {
        return adminRentOrderService.getExpressTrace(rentOrderId);
    }

    /**
     * 后台修改预约上门时间
     *
     * @param returnBookDto
     * @return
     */
    @PostMapping("/updateSubscribeTime")
    public Object updateSubscribeTime(@RequestBody ReturnBookDto returnBookDto) {
        adminRentOrderService.updateSubscribeTime(returnBookDto);
        return ResponseUtil.ok();
    }

    @PostMapping("/returnConfirm/{id}")
    public Object returnConfirm(@PathVariable(value = "id") Integer rentOrderId) {
        return adminRentOrderService.rentBackConfirm(rentOrderId);
    }

    /**
     * 管理员终止还书流程
     * @param rentOrderId
     * @return
     */
    @PostMapping("/adminReturnConfirm/{id}")
    public Object adminReturnConfirm(@PathVariable(value = "id") Integer rentOrderId) {
        return adminRentOrderService.adminReturnConfirm(rentOrderId);
    }

    // todo 建表
    //  修改状态是时刻注入新表
    //  查询新表 封装list

}
